<?php
/**
 *  库存管理
 *  InvOiController.php
 *  Create On 2021/9/8
 *  Create by zl
 */
namespace App\Http\Controllers\Scm;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class InvOiController extends Controller
{
    public function get_ware_data(Request $request)
    {
        $this->checkpurview(11);
        $data = $request->all();
        $page = $data['page'] ?? 1;
        $size = $data['pageSize'] ?? 20;
        $showZero   = !empty($data['showZero']) ? ($data['showZero'] == 'true' ? 1 : 0) : 0;
        $categoryId = $data['categoryId'] ?? 0;
        $locationId = $data['locationId']  ?? 0;
        $goods = $this->str_enhtml(($data['goods'] ?? ''));
        $where = '(a.isDelete=0)';
        $where .= strlen($goods)>0 ? ' and (b.name like "%'.$goods.'%" or b.barCode = '.$goods.')' : '';
        $where .= $locationId>0 ? ' and a.locationId='.$locationId : '';
        if ($categoryId > 0) {
            $cid = array_column(DB::table('category')->whereRaw('(1=1) and find_in_set('.$categoryId.',path)')->get()->toArray(),'id');
            if (count($cid)>0) {
                $cid = join(',',$cid);
                $where .= ' and b.categoryId in('.$cid.')';
            }
        }
        $where .= $this->get_location_purview();
        $having = $showZero == 1 ? ' HAVING qty=0' : '';
        $list = $this->get_inventory($where.' GROUP BY a.invId,a.locationId '.$having.' limit '.$size*($page-1).','.$size);
        foreach ($list as $arr=>$row) {
            $v[$arr]['assistName']    = $row['categoryName'];
            $v[$arr]['barCode']       = $row['barCode'];
            $v[$arr]['invSpec']       = $row['invSpec'];
            $v[$arr]['locationId']    = $locationId > 0 ? intval($row['locationId']) : 0;
            $v[$arr]['skuName']       = '';
            $v[$arr]['qty']           = (float)$row['qty'];
            $v[$arr]['locationName']  = $row['locationName'];
            $v[$arr]['assistId']      = 0;
            $v[$arr]['invCost']       = 0;
            $v[$arr]['unitName']      = $row['unitName'];
            $v[$arr]['skuId']         = 0;
            $v[$arr]['invId']         = intval($row['invId']);
            $v[$arr]['invNumber']     = $row['invNumber'];
            $v[$arr]['invName']       = $row['invName'];
        }
        $json['msg']    = 'success';
        $json['data']['page']         = $page;
        $json['data']['records']      = $this->get_inventory($where.' GROUP BY a.invId,a.locationId'.$having,3)[0]['num'] ?? 0;
        $json['data']['total']        = ceil($json['data']['records']/$size);
        $json['data']['data']         = isset($v) ? $v : array();
        $this->str_alert(200,'获取成功',$json['data']);
    }
}
